"""
Case Type   : 全密态
Case Name   : 正常启动数据库，验证主密钥algorithm参数
Create At   : 2022/1/24
Owner       : opentestcase020
Description :
    1、配置$GAUSSHOME/etc/localkms路径
    2、连接数据库，创建cmk
    3、清理环境
Expect      :
    1、配置成功
    2、成功
    3、成功
History     :
    Modified by peilinqian at 2025-1-22:优化用例配置$GAUSSHOME/etc/localkms路径步骤，排除app路径拼接错误问题
    2023/09/07 modify by opentestcase024: 研发代码变更加密算法变更为rsa_3072，优化用例
"""
import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class FullDensityState(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.Constant = Constant()
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.cmk = 'cmk_security_qualityquery_0002'
        self.key_path = 'keypath_security_qualityquery_0002'

    def test_create_cmk_cek(self):
        self.logger.info(f'-----{os.path.basename(__file__)}' + ' start-----')
        text = '---step1:配置$GAUSSHOME/etc/localkms路径;expect:成功-----'
        self.logger.info(text)
        mkdir_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'if [ ! -d "$GAUSSHOME/etc/localkms" ];' \
                    f'then mkdir $GAUSSHOME/etc/localkms;fi'
        self.logger.info(mkdir_cmd)
        self.userNode.sh(mkdir_cmd).result()
        text = '---step2:连接数据库，创建cmk;expect：成功---'
        self.logger.info(text)
        sql_cmd = f'''drop client master key if exists {self.cmk};
            create client master key {self.cmk} with
            (key_store=localkms,key_path="{self.key_path}",
            algorithm=rsa_3072);
            drop client master key if exists {self.cmk};
            create client master key {self.cmk} with
            (key_store=localkms,key_path="{self.key_path}",
            algorithm=rsa_3072);
            drop client master key if exists {self.cmk};
            create client master key {self.cmk} with
            (key_store=localkms,key_path="{self.key_path}",
            algorithm=sm2);
            '''
        self.logger.info(sql_cmd)
        msg1 = self.sh_primy.execut_db_sql(sql_cmd, sql_type='-C')
        self.logger.info(msg1)
        self.assertTrue(msg1.count('CREATE CLIENT MASTER KEY') == 3,
                        text + '执行失败')

    def tearDown(self):
        text = '--step3:清理环境;expect:成功-------'
        self.logger.info(text)
        sql_cmd = f'drop client master key {self.cmk} cascade;'
        self.logger.info(sql_cmd)
        msg1 = self.sh_primy.execut_db_sql(sql_cmd, sql_type='-C')
        self.logger.info(msg1)
        text = '----删除文件----'
        self.logger.info(text)
        clean_cmd = f"source {macro.DB_ENV_PATH};" \
                    f"rm -rf $GAUSSHOME/etc/localkms;" \
                    f"ls $GAUSSHOME/etc/localkms"
        clean_msg = self.userNode.sh(clean_cmd).result()
        self.logger.info(clean_msg)
        self.assertIn(self.Constant.drop_cmk_success, msg1, text + '执行失败')
        self.assertIn('No such file or directory', clean_msg, text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)}' + ' end-----')












